/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-07 10:48:02
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-07 10:48:33
 * @FilePath: /webServer/src/service/jwt.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Config, Inject, Provide } from '@midwayjs/core';
import { Jwt, JwtService as JwtDefaultService } from '@midwayjs/jwt';
import { JwtConfigInterface } from '../interface';

@Provide()
export class JwtService {
  @Inject()
  jwtService: JwtDefaultService;

  @Config('jwt')
  jwtConfig: JwtConfigInterface;

  /**
   * 签出token
   * @param params
   * @returns
   */
  public signSync(params?: object) {
    return this.jwtService.signSync({ ...params }, this.jwtConfig.secret, {
      expiresIn: this.jwtConfig.expiresIn,
      algorithm: this.jwtConfig.algorithm,
    });
  }

  /**
   * 验证token
   * @param token string
   * @returns {Jwt.Jwt | string}
   */
  public verifySync(token: string): Jwt.Jwt | string {
    return this.jwtService.verifySync(token, {
      complete: true,
    });
  }
}
